package com.cqut.hfh.ordering.service.Impl;

import com.cqut.hfh.ordering.pojo.FodFood;
import com.cqut.hfh.ordering.pojo.FoodUser;
import com.cqut.hfh.ordering.service.IFodUserService;
import com.cqut.hfh.ordering.utils.EntityIdutils;
import com.cqut.hfh.ordering.utils.Message;
import com.cqut.li.mybatisutil.dao.base.Dao;
import com.cqut.li.mybatisutil.dao.base.SearchDao;
import com.cqut.li.mybatisutil.entity.base.Entity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author LJH
 * @date 2019/6/25-23:20
 */
@Service
public class FodUserServiceImpl implements IFodUserService {

    @Resource
    private Dao dao;
    @Resource
    private SearchDao searchDao;

    @Override
    public FoodUser login(String user_name, String user_pwd) {
        String condition = " user_name = '" + user_name + "'";

        List<Map<String, Object>> temp_map = dao.findByCondition(new String[]{
                        "user_pwd",
                        "user_name",
                        "user_id"},
                condition,
                FoodUser.class);

        if(temp_map == null) {
            return null;
        }

        for( Map<String, Object> map : temp_map ){
            //密码判断
            if(map.get("user_pwd").toString().equals(user_pwd)){

                FoodUser u = new FoodUser();
                u.setProperties(map);
                return u;
            }
        }

        return null;
    }

    @Override
    public int register(FoodUser user) {
        List<FoodUser> byCondition = dao.getByCondition(" user_card='" + user.getUser_card() + "'", FoodUser.class);
        if(byCondition.size() > 0){
            return 2;
        }
        user.setUser_id(EntityIdutils.getId());
        return dao.save(user);
    }

    @Override
    public List<Map<String, Object>> getFodUsers(String limit, String page) {
        String con = " 1 = 1 limit "
                + (Integer.valueOf(page)-1)*Integer.valueOf(limit) + "," + limit ;
        List<Map<String, Object>> users_list
                = dao.findByCondition(new String[]{"user_id",
                        "user_name",
                        "user_pwd",
                        "user_phone",
                        "user_card",
                        "optime",
                        "oper",
                        "isva"},
                con,
                FoodUser.class);
        return users_list;
    }

    @Override
    public int getUsersCount() {
        return searchDao.sqlCount(Entity.getTableName(FoodUser.class));
    }

    @Override
    public Map<String, Object> findUserById(String user_id) {
        return dao.getByID(user_id, FoodUser.class).toMap();
    }

    @Override
    public List<Map<String, Object>> getUsersByName(String s, String page, String user_name) {
        String con = " user_name like '%" + user_name + "%' limit "
                + (Integer.valueOf(page)-1)*Integer.valueOf(s) + "," + s ;
        List<Map<String, Object>> users_list
                = dao.findByCondition(new String[]{"user_id",
                        "user_name",
                        "user_pwd",
                        "user_phone",
                        "user_card",
                        "optime",
                        "oper",
                        "isva"},
                con,
                FoodUser.class);
        return users_list;
    }

    @Override
    public int delById(String user_id) {
        //  dao.deleteByCondition(" user_id = '" + user_id + "'", FoodUser.class);
        return dao.deleteByID(user_id, FoodUser.class);
        //  return dao.deleteByID(user_id, FoodUser.class);
    }

    @Override
    public int updateUser(FoodUser user) {
        int a = dao.updatePropByID(user, user.getUser_id());

        return 1;
    }

    @Override
    public int saveUser(FoodUser user) {
        int save = dao.save(user);

//        String[] mod_strs = module_ids.split(",");
//
//        for( String mod_str : mod_strs ){
//            EhyRoleModule rm = new EhyRoleModule();
//            rm.setRole_id(role.getRole_id());
//            rm.setRm_id(EntityIdutils.getId());
//            rm.setMod_id(mod_str);
//            dao.save(rm);
//        }
//        FoodUser fu = new FoodUser();
//        fu.setUser_id(user.getUser_id());
//        dao.save(fu);

        return save;
    }

}
